<template>
  <div class="needs-container">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-input
        v-model="searchValue"
        placeholder="搜索求购信息"
        clearable
        class="search-input"
        @keyup.enter.native="handleSearch">
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="handleSearch">
        </el-button>
      </el-input>
      
      <div class="hot-search-tags">
        <span class="hot-title">热门搜索：</span>
        <el-tag
          v-for="(tag, index) in hotTags"
          :key="index"
          class="hot-tag"
          @click="handleTopicDetail(tag)">
          {{ tag }}
        </el-tag>
      </div>
    </div>

    <!-- 求购列表 -->
    <div class="needs-list">
      <el-card
        v-for="(item, index) in cneeds"
        :key="index"
        class="needs-card"
        shadow="hover"
        @click.native="detailsClick(item.orderId)">
        
        <div class="needs-header">
          <el-tag
            size="small"
            type="warning"
            v-if="item.type === 'needs'">
            求购
          </el-tag>
          <h4 class="needs-title">{{ item.title }}</h4>
        </div>
        
        <p class="needs-content">{{ item.content }}</p>
        
        <div class="needs-footer">
          <span class="update-time">
            <i class="el-icon-time"></i>
            {{ item.updateTime | formatTimer }}
          </span>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchValue: '',
      hotTags: [
        '苹果', '新疆哈密瓜', '樱桃', 
        '西红柿', '水稻', '玉米', '赣南脐橙'
      ]
    };
  },
  filters: {
    formatTimer(value) {
      const date = new Date(value);
      const y = date.getFullYear();
      const MM = (date.getMonth() + 1).toString().padStart(2, '0');
      const d = date.getDate().toString().padStart(2, '0');
      return `${y}-${MM}-${d}`;
    }
  },
  props: {
    cneeds: {
      type: Array,
      required: true
    }
  },
  methods: {
    detailsClick(orderId) {
      this.$store.commit("updateOrderId", orderId);
      this.$router.push(`/home/purchaseDetails?orderId=${orderId}`)
        .catch(err => console.error(err));
    },
    handleSearch() {
      this.$emit('handleSearch', this.searchValue);
    },
    handleTopicDetail(tag) {
      this.searchValue = tag;
      this.handleSearch();
    }
  }
};
</script>

<style lang="less" scoped>
.needs-container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
  
  .search-section {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    margin-bottom: 20px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    
    .search-input {
      width: 400px;
      margin-bottom: 15px;
      
      /deep/ .el-input-group__append {
        background-color: #67C23A;
        color: white;
        border-color: #67C23A;
      }
    }
    
    .hot-search-tags {
      .hot-title {
        color: #909399;
        font-size: 14px;
        margin-right: 10px;
      }
      
      .hot-tag {
        margin-right: 10px;
        cursor: pointer;
        background-color: #f0f9eb;
        color: #67C23A;
        border-color: #e1f3d8;
        
        &:hover {
          background-color: #e1f3d8;
        }
      }
    }
  }
  
  .needs-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
    gap: 20px;
  }
  
  .needs-card {
    cursor: pointer;
    transition: all 0.3s;
    border-radius: 6px;
    
    &:hover {
      transform: translateY(-3px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
    
    .needs-header {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      
      .needs-title {
        margin: 0 0 0 10px;
        font-size: 16px;
        color: #303133;
        flex: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    
    .needs-content {
      margin: 0;
      color: #606266;
      font-size: 14px;
      line-height: 1.5;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
    }
    
    .needs-footer {
      margin-top: 15px;
      display: flex;
      justify-content: flex-end;
      
      .update-time {
        font-size: 12px;
        color: #909399;
        
        i {
          margin-right: 5px;
        }
      }
    }
  }
}
</style>